/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dacnpm;

import java.sql.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author SONY
 */
public class Form_QuanLyCauHoi2 extends javax.swing.JFrame {
    Statement stt, stt2;
    Connection conn, conn2;
    ResultSet rs, rs2;
    ResultSetMetaData metadata, metadata2;
    int n=0;

    /**
     * Creates new form Form_QuanLyCauHoi2
     */
    public Form_QuanLyCauHoi2() throws Exception {
        initComponents();
        LoadDB();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lcauhoi = new java.awt.List();
        bt24_edit = new javax.swing.JButton();
        bt24_finish = new javax.swing.JButton();
        bt24_logout = new javax.swing.JButton();
        lst_cauhoi = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setPreferredSize(new java.awt.Dimension(800, 600));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        jPanel1.add(lcauhoi, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 240, 570, 290));

        bt24_edit.setText("Delete");
        bt24_edit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt24_editActionPerformed(evt);
            }
        });
        jPanel1.add(bt24_edit, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 260, 80, 40));

        bt24_finish.setText("Finish");
        bt24_finish.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt24_finishActionPerformed(evt);
            }
        });
        jPanel1.add(bt24_finish, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 550, 130, 40));

        bt24_logout.setText("Log Out");
        bt24_logout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt24_logoutActionPerformed(evt);
            }
        });
        jPanel1.add(bt24_logout, new org.netbeans.lib.awtextra.AbsoluteConstraints(703, 570, 80, 30));

        lst_cauhoi.setIcon(new javax.swing.ImageIcon(getClass().getResource("/dacnpm/mslide2_4.jpg"))); // NOI18N
        jPanel1.add(lst_cauhoi, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bt24_editActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt24_editActionPerformed
        // TODO add your handling code here:
        String st=lcauhoi.getSelectedItem();
        String sql2="Delete from question1 where name_question="+st;
        try {
        lcauhoi.remove(lcauhoi.getSelectedItem());
        stt.executeQuery(sql2);
        
        } catch (SQLException ex) {
            //Logger.getLogger(Form_QuanLyUser2.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_bt24_editActionPerformed

    private void bt24_finishActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt24_finishActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        try {
            new Form_QuanTri().setVisible(true);
        } catch (Exception ex) {
            Logger.getLogger(Form_QuanTri.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_bt24_finishActionPerformed

    private void bt24_logoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt24_logoutActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        try {
            new Form_DangNhap().setVisible(true);
        } catch (Exception ex) {
            Logger.getLogger(Form_QuanLyCauHoi2.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_bt24_logoutActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[])throws Exception {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Form_QuanLyCauHoi2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Form_QuanLyCauHoi2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Form_QuanLyCauHoi2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Form_QuanLyCauHoi2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {                
                    new Form_QuanLyCauHoi2().setVisible(true);
                } catch (Exception ex) {
                    Logger.getLogger(Form_QuanLyCauHoi2.class.getName()).log(Level.SEVERE, null, ex);
                }
                
            }
        });
    }
        private void LoadDB() throws Exception{
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        String url = "Jdbc:odbc:User";
        conn = DriverManager.getConnection(url);
        stt = conn.createStatement();
        //dfTable = new DefaultTableModel();
        String sql = "Select name_question  from question1";

        rs = stt.executeQuery(sql);

        ArrayList<String> cauhoi = new ArrayList<>();
        while (rs.next()) {
            cauhoi.add(rs.getString("name_question"));
            n++;
        }
        for (int i = 0; i < n; i++) {
            lcauhoi.add(cauhoi.get(i), i);
            //lname.add(user.get(i));
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt24_edit;
    private javax.swing.JButton bt24_finish;
    private javax.swing.JButton bt24_logout;
    private javax.swing.JPanel jPanel1;
    private java.awt.List lcauhoi;
    private javax.swing.JLabel lst_cauhoi;
    // End of variables declaration//GEN-END:variables
}
